package com.fdb.efp.riskm.api.rest;

/**
 *  *
 * @author luzhaoyu
 * @version 0.1
 * @date 2019/9/15
 * @since 2019/9/15
 *
 */

import java.util.*;

import com.alibaba.fastjson.JSONObject;
import com.fdb.efp.riskm.api.service.RiskmUtilService;
import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.basic.framework.core.constant.DataRuleType;
import com.fdb.basic.framework.core.constant.PubConstant;
import com.fdb.basic.framework.core.constant.ResponseData;
import com.fdb.basic.framework.core.util.StringUtil;
import com.fdb.basic.framework.core.util.TimeUtil;
import com.fdb.basic.sequence.service.facade.SequenceService;
import com.fdb.efp.console.service.facade.PubSysInfoService;
import com.fdb.efp.e4a.service.facade.SRoleDataRuleService;
import com.fdb.efp.riskm.common.RiskwarningConstant;
import com.fdb.efp.riskm.service.facade.*;
import com.fdb.efp.riskm.service.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.fdb.basic.framework.web.controller.AbstractController;


@RestController
@RequestMapping("/api")
public class PspSpecCheckAppController extends AbstractController implements FrameworkService {

    @Autowired
    private SequenceService sequenceService;
    @Autowired
    private RiskmUtilService riskmUtilService;
    @Autowired
    @Qualifier("pubSysInfoService")
    PubSysInfoService pubSysInfoService;
    @Autowired
    @Qualifier("sRoleDataRuleService")
    private SRoleDataRuleService sRoleDataRuleService;
    @Autowired
    @Qualifier("pspSpecCheckCusService")
    private PspSpecCheckCusService pspSpecCheckCusService;
    @Autowired
    @Qualifier("pspSpecCheckAppService")
    private PspSpecCheckAppService pspSpecCheckAppService;


    /**
     * 列表数据查询
     *
     * @param page
     * @param size
     */
    @RequestMapping(value = "/psp/spec/check/apps", method = RequestMethod.POST)
    public @ResponseBody
    ResponseData<List<PspSpecCheckAppVO>> queryPspSpecCheckAppAll(
            @RequestBody PspSpecCheckAppVO vo) {
        List<PspSpecCheckAppVO> outputVo = null;
        // 根据用户角色获取用户权限方案
        Map<String, String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
        vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
        DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
        setUserInfoToVO(vo);
        if (!StringUtil.isNullorBank(vo.getExportFlag()) && "exp".equals(vo.getExportFlag())) {
            logger.info("开始导出数据！");
            switch (dataRuleType) {
                case Owner:
                    outputVo = pspSpecCheckAppService.queryExAllOwner(vo);
                    break;
                case CurrOrg:
                    outputVo = pspSpecCheckAppService.queryExAllCurrOrg(vo);
                    break;
                case CurrDownOrg:
                    outputVo = pspSpecCheckAppService.queryExAllCurrDownOrg(vo);
                    break;
                default:
                    outputVo = pspSpecCheckAppService.queryExAllOwner(vo);
                    break;
            }
        } else {
            logger.info("开始查询数据！");
            // 根据权限方案调用不同的方案进行数据查询
            switch (dataRuleType) {
                case Owner:
                    outputVo = pspSpecCheckAppService.queryAllOwner(vo);
                    break;
                case CurrOrg:
                    outputVo = pspSpecCheckAppService.queryAllCurrOrg(vo);
                    break;
                case CurrDownOrg:
                    outputVo = pspSpecCheckAppService.queryAllCurrDownOrg(vo);
                    break;
                default:
                    outputVo = pspSpecCheckAppService.queryAllOwner(vo);
                    break;
            }
        }
        riskmUtilService.displayUserName(outputVo,"createUser","createUserName");
        return getResponseData(outputVo);
    }


    /**
     * 根据主键查询详情
     *
     * @return
     */
    @RequestMapping(value = "/psp/spec/check/app/oneInfo", method = RequestMethod.POST)
    public @ResponseBody
    ResponseData<PspSpecCheckAppVO> queryByPk(@RequestBody PspSpecCheckAppVO vo) {
        PspSpecCheckAppVO inVo = new PspSpecCheckAppVO();
        inVo.setBizSerno(vo.getBizSerno());
        PspSpecCheckAppVO outputVo = pspSpecCheckAppService.queryByPk(inVo);
        return getResponseData(outputVo);

    }

    /**
     * 根据主键删除信息
     * 修改人 luzhaoyu
     * 修改时间 2019/9/29
     * @param inPspSpecCheckAppVo
     * @return
     */
    @RequestMapping(value = "/psp/spec/check/app", method = RequestMethod.DELETE)
    public @ResponseBody
    ResponseData<Integer> deleteByPk(@RequestBody PspSpecCheckAppVO inPspSpecCheckAppVo) {
        int outputVo = pspSpecCheckAppService.deleteByPk(inPspSpecCheckAppVo);
        if(outputVo == -1){
            return getResponseData(outputVo);
        }
        // 同时删除PspSpecCheckCusVO中的数据
        PspSpecCheckCusVO pspSpecCheckCusVO = new PspSpecCheckCusVO();
        pspSpecCheckCusVO.setBizSerno(inPspSpecCheckAppVo.getBizSerno());
        outputVo = pspSpecCheckCusService.deleteByPk(pspSpecCheckCusVO);
        return getResponseData(outputVo);

    }

    /**
     * 根据主键更新信息
     *
     * @param inPspSpecCheckAppVo
     * @return
     */
    @RequestMapping(value = "/psp/spec/check/app", method = RequestMethod.PUT)
    public @ResponseBody
    ResponseData<Integer> updateByPk(@RequestBody PspSpecCheckAppVO inPspSpecCheckAppVo) {
        int outputVo = pspSpecCheckAppService.updateByPk(inPspSpecCheckAppVo);
        return getResponseData(outputVo);

    }

    /**
     * 新增数据
     *
     * @param inPspSpecCheckAppVo
     * @return
     */
    @RequestMapping(value = "/psp/spec/check/app", method = RequestMethod.POST)
    public @ResponseBody
    ResponseData<Integer> insertPspSpecCheckApp(@RequestBody PspSpecCheckAppVO inPspSpecCheckAppVo) {
        PspSpecCheckAppVO pspSpecCheckAppVO = new PspSpecCheckAppVO();
        try {
            pspSpecCheckAppVO.setBizSerno(sequenceService.getSequence("BIZ_SERNO", "null", "null"));
            pspSpecCheckAppVO.setCreateUser(inPspSpecCheckAppVo.getCreateUser());
            pspSpecCheckAppVO.setCreateTime(TimeUtil.getCurrentDateTime());
            pspSpecCheckAppVO.setInputBrId(inPspSpecCheckAppVo.getInputBrId());
            pspSpecCheckAppVO.setChkName(inPspSpecCheckAppVo.getChkName());
            pspSpecCheckAppVO.setSpecCheckStatus(RiskwarningConstant.SPEC_CHECK_STATUS_00);
        } catch (Exception e) {
            e.printStackTrace();
        }
        int outputVo = pspSpecCheckAppService.insertPspSpecCheckApp(inPspSpecCheckAppVo);
        return getResponseData(outputVo);

    }

    /**
     * 提交数据
     *
     * @param SubmitByPk
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/psp/spec/check/submit", method = RequestMethod.POST)
    public @ResponseBody
    ResponseData<Integer> submitByPk(@RequestBody PspSpecCheckAppVO inPspSpecCheckAppVo) throws Exception {
        logger.info("专项检查新增数据，入参参数为：" + JSONObject.toJSONString(inPspSpecCheckAppVo));

        return getResponseData(1);
    }

    /**
     * 导出功能查询数据
     *
     * @param page
     * @param size
     */
    @RequestMapping(value = "/psp/spec/check/app/sign", method = RequestMethod.POST)
    public @ResponseBody
    ResponseData<List<PspSpecCheckAppVO>> querySignSearchAll(
            @RequestBody PspSpecCheckAppVO vo) {
        List<PspSpecCheckAppVO> outputVo = Optional.ofNullable(pspSpecCheckAppService.querySignSearchAll(vo)).orElseGet(ArrayList::new);
        return getResponseData(outputVo);
    }

}

